package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BatteryModelChart;
import com.tbit.uqbike.object.pojo.dto.BatteryModelChartDTO;
import com.tbit.uqbike.object.pojo.dto.BatteryModelChartUpdateDTO;
import com.tbit.uqbike.object.pojo.vo.BatteryModelChartVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BatteryModelChartService;
import com.tbit.uqbike.webmanager.dao.core.BatteryModelChartDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author:BUGTian
 * @DATE: 2022/5/10
 */
@Service
public class BatteryModelChartServiceImpl implements BatteryModelChartService {

    @Autowired
    BatteryModelChartDao batteryModelChartDao;


    @Override
    public Result insert(BatteryModelChartDTO batteryModelChartDTO, LoginInfo loginInfo) {
        //参数校验
        check(batteryModelChartDTO, loginInfo);
        //根据型号和品牌id查询是型号名称是否重复
        int count = batteryModelChartDao.selectCountByBatteryModel(batteryModelChartDTO.getBatteryModel(), batteryModelChartDTO.getAccountIds()[0]);
        if (count > 0) {
            return Result.error("添加失败，该电池型号已存在");
        }
        BatteryModelChart batteryModelChart = new BatteryModelChart();
        BeanUtils.copyProperties(batteryModelChartDTO, batteryModelChart);
        Date addTime = new Date();
        for (Integer accountId : batteryModelChartDTO.getAccountIds()) {
            batteryModelChart
                    //用户id
                    .setAddAccountUserId(loginInfo.getAccountUserId())
                    //品牌id
                    .setAccountId(accountId)
                    .setAddTime(addTime);
            batteryModelChartDao.insert(batteryModelChart);
        }
        return Result.success();
    }


    @Override
    public PageResult<BatteryModelChartVO> queryPage(Integer page, Integer size, Integer[] accountIds) {
        //根据品牌id分页查询
        PageInfo<BatteryModelChartVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> batteryModelChartDao.queryByAccountIds(accountIds));
        return new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public Result update(BatteryModelChartUpdateDTO batteryModelChartUpdateDTO, LoginInfo loginInfo) {
        batteryModelChartUpdateDTO.setUpdateAccountUserId(loginInfo.getAccountUserId());
        batteryModelChartUpdateDTO.setUpdateTime(new Date());
        batteryModelChartDao.updateByBatteryModelId(batteryModelChartUpdateDTO);
        return Result.success();
    }

    @Override
    public Result delete(Integer[] batteryModelIds) {
        if (batteryModelIds == null || batteryModelIds.length < 1) {
            return Result.error("电池型号id不能为空!");
        }
        //查询电池型号绑定电池数量
        int boundCount = batteryModelChartDao.selectBoundCount(batteryModelIds);
        if (boundCount > 0) {
            //有绑定关系
            return Result.error("删除失败，电池型号已被绑定！");
        }
        batteryModelChartDao.deleteByBatteryModelId(batteryModelIds);
        return Result.success();
    }


    /**
     * 参数校验
     *
     * @param batteryModelChartDTO
     */
    private void check(BatteryModelChartDTO batteryModelChartDTO, LoginInfo loginInfo) {
        if (StringUtils.isEmpty(batteryModelChartDTO.getBatteryBrand())) {
            throw new BaseException("电池品牌不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getBatteryModel())) {
            throw new BaseException("电池型号不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getBatteryCapacity())) {
            throw new BaseException("电池容量不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getBatteryU())) {
            throw new BaseException("电池电压不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getBatteryI())) {
            throw new BaseException("电池电流不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getMaterials())) {
            throw new BaseException("材料不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getMaterials())) {
            throw new BaseException("满电电压不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getMaterials())) {
            throw new BaseException("馈电电压不能为空");
        }
        if (StringUtils.isEmpty(batteryModelChartDTO.getMaterials())) {
            throw new BaseException("最大续航里程不能为空");
        }
        Integer[] accountIds = batteryModelChartDTO.getAccountIds();
        if (accountIds == null || accountIds.length < 1) {
            throw new BaseException("请先选择品牌");
        }
    }
}
